package cn.stylefeng.guns.modular.bi.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.core.util.DateUtil;
import cn.stylefeng.guns.modular.basic.service.BasicOperateService;
import cn.stylefeng.guns.modular.bi.entity.BiCostDiff;
import cn.stylefeng.guns.modular.bi.model.params.BiCostDiffParam;
import cn.stylefeng.guns.modular.bi.model.result.BiCostDiffResult;
import cn.stylefeng.guns.modular.bi.service.BiCostDiffService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.model.result.DictResult;
import cn.stylefeng.guns.sys.modular.system.service.DictService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.List;


/**
 * 运费成本差异控制器
 *
 * @author zhangzh
 * @Date 2024-05-10 14:24:16
 */
@Controller
@RequestMapping("/biCostDiff")
public class BiCostDiffController extends BaseController {

    private String PREFIX = "/bi/biCostDiff";

    @Autowired
    private BiCostDiffService biCostDiffService;

    @Autowired
    private UserService userService;

    @Autowired
    private DictService dictService;

    @Autowired
    private BasicOperateService basicOperateService;

    /**
     * 跳转到主页面
     *
     * @author zhangzh
     * @Date 2024-05-10
     */
    @RequestMapping("")
    public String index(Model model) {
        User user = userService.getcurrentUser();
        Long deptId = user.getDeptId();
        Long deliverId = user.getDeliverId();
        model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId, deliverId, user.getOperateId()));
        return PREFIX + "/biCostDiff.html";
    }

    /**
     * 查询列表
     *
     * @author zhangzh
     * @Date 2024-05-10
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(BiCostDiffParam biCostDiffParam, String condition) {
        Long deptId = userService.getDeptId();
        if (deptId != null) {
            biCostDiffParam.setDeptId(deptId);
        }

        if (StringUtils.isBlank(biCostDiffParam.getStartTime())) {
            biCostDiffParam.setStartTime(DateUtil.getDate());
        }
        if (StringUtils.isBlank(biCostDiffParam.getEndTime())) {
            biCostDiffParam.setEndTime(DateUtil.getDate());
        }
        List<DictResult> dictList = dictService.findByDicttypeCode2("COST_RAT");//成本标准比例
        if (dictList != null && dictList.size() > 0) {
            biCostDiffParam.setCostRat(new BigDecimal(dictList.get(0).getName()));
        }

        if (biCostDiffParam.getProId() == null) {
            biCostDiffParam.setProId(-1L);
        }

        return this.biCostDiffService.findPageBySpec(biCostDiffParam, condition);
    }

    /**
     * 查询列表
     *
     * @author zhangzh
     * @Date 2024-05-10
     */
    @ResponseBody
    @RequestMapping("/listAll")
    public List<BiCostDiffResult> listAll(BiCostDiffParam biCostDiffParam) {
        Long deptId = userService.getDeptId();
        if (deptId != null) {
            biCostDiffParam.setDeptId(deptId);
        }
        if (StringUtils.isBlank(biCostDiffParam.getStartTime())) {
            biCostDiffParam.setStartTime(DateUtil.getDate());
        }
        if (StringUtils.isBlank(biCostDiffParam.getEndTime())) {
            biCostDiffParam.setEndTime(DateUtil.getDate());
        }
        List<DictResult> dictList = dictService.findByDicttypeCode2("COST_RAT");//成本标准比例
        if (dictList != null && dictList.size() > 0) {
            biCostDiffParam.setCostRat(new BigDecimal(dictList.get(0).getName()));
        }


        if (biCostDiffParam.getProId() == null) {
            biCostDiffParam.setProId(-1L);
        }

        return this.biCostDiffService.findListBySpec(biCostDiffParam);
    }
}


